﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CurriculumnManage.DBAccess;
using CurriculumnManage.Helper;
using CurriculumnManage.Modal;
using System.IO;

namespace CurriculumnManage
{
    public partial class frmCurriCulumnList : Form
    {
        private int currCurriCulumnStatusId = 0;
        private int currSubjectProfileId = 0;
        private int currSubjectGradeProfileId = 0;
        public frmCurriCulumnList()
        {
            InitializeComponent();
            this.btnSentMail.Enabled = false;
            this.btnUpdate.Enabled = false;
        }

        private void cmbGrade_SelectedIndexChanged(object sender, EventArgs e)
        {
            //try
            //{
            //    if (!CommonFunction.CheckYear(this.txtYear)) return;
            //    ClearGridData();
            //}
            //catch (Exception ex)
            //{
            //    CommonFunction.ShowError("frmCurriCulumnList.cmbGrade_SelectedIndexChanged", ex.Message);
            //}
        }


        private void btnView_Click(object sender, EventArgs e)
        {
            try
            {
                if (!CommonFunction.CheckYear(this.txtYear)) return;
                if (!CommonFunction.CheckGrade(this.cmbGrade)) return;
                //AddCulumnHader();
                ShowSubjectData();
                if ((this.gvSubject.DataSource as List<SubjectEntiy>).Count == 0)
                {
                    this.gvSubject.DataSource = new List<SubjectEntiy>();
                    this.gvCurriCulumn.DataSource = new List<CurriCulumnEntity>();
                    this.listTeacher.DataSource = null;
                    CommonFunction.ShowMsg("データがないです");
                    this.btnSentMail.Enabled = false;
                    this.btnUpdate.Enabled = false;
                }
                if (gvSubject.SelectedRows.Count > 0)
                {
                    ShowCurriCulumnData();
                    ShowListTeacher();
                    this.cmbStatus2.Text = gvSubject["status1", gvSubject.CurrentCell.RowIndex].Value.ToString();
                    this.btnUpdate.Enabled = true;
                    this.btnSentMail.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                CommonFunction.ShowError("frmCurriCulumnList.btnView_Click", ex.Message);
            }
        }

        private void ShowSubjectData()
        {
            try
            {
                List<SubjectEntiy> listSubject = new List<SubjectEntiy>();
                using (DBContext db = new DBContext())
                {
                    int? subjectSelected = int.Parse(this.cmbSubjectSelected.SelectedValue.ToString());
                    if (subjectSelected == -1) subjectSelected = null;
                    int? crriStatus = int.Parse(this.cmbSelectStatus.SelectedValue.ToString());
                    if (crriStatus == 0||crriStatus ==1) crriStatus = null;
                    SqlResult result = db.CallProcedure("usp_subject_getbyYearAndGrade", int.Parse(this.txtYear.Text), int.Parse(this.cmbGrade.Text), subjectSelected, crriStatus);
                    if (result.DataSet.Tables.Count > 0 && result.DataSet.Tables[0].Rows.Count > 0)
                    {
                        DataTable dt = result.DataSet.Tables[0];
                        int index = 0;
                        foreach (DataRow dr in dt.Rows)
                        {
                            //検索状況が未入力時
                            if (this.cmbSelectStatus.Text == EumnCurriLumnStatus.未入力.ToString())
                            {
                                if(MyUtils.ParseToInt(dr["istatusID"], 0)!=1 &&MyUtils.ParseToInt(dr["istatusID"], 0)!=0)
                                    continue;
                            }
                            SubjectEntiy tempEntiy = new SubjectEntiy
                            {
                                No = ++index,
                                SubjectName = dr["vSubjectName"].ToString(),
                                Status = CommonFunction.GetStatusText(MyUtils.ParseToInt(dr["istatusID"], 0)),
                                RecordPeople = MyUtils.ParseToString(dr["vUpdatedBy"], ""),
                                SubjectGradeProfileID = int.Parse(dr["iSubjectGradeProfileID"].ToString()),
                                CurriCulumnStatusID = MyUtils.ParseToInt(dr["iCurriCulumnStatusID"], 0),
                                SubjectProfileID = MyUtils.ParseToInt(dr["iSubjectProfileID"], 0),
                                SBOfile = MyUtils.ParseToString(dr["vGISSBOPath"],"")
                            };
                            listSubject.Add(tempEntiy);
                        }

                        //this.gvSubject.ClearSelection();
                    }
                    this.gvSubject.DataSource = listSubject;
                    if (gvSubject.SelectedRows.Count > 0)
                        this.currCurriCulumnStatusId = MyUtils.ParseToInt(gvSubject["CurriCulumnStatusID", gvSubject.CurrentCell.RowIndex].Value.ToString(), 0);
                    else
                        this.currCurriCulumnStatusId = 0;

                    if (this.currCurriCulumnStatusId == 0)
                        this.cmbStatus2.Text = "";
                }
            }
            catch (Exception ex)
            {
                CommonFunction.ShowError("frmCurriCulumnList.ShowSubjectData", ex.Message);
            }
        }
        private void ShowCurriCulumnData()
        {
            try
            {
                List<CurriCulumnEntity> listCulumn = new List<CurriCulumnEntity>();
                using (DBContext db = new DBContext())
                {
                    int currRow = this.gvSubject.CurrentCell.RowIndex;
                    SqlResult result = db.CallProcedure("usp_webCurriCulumnInfo_getBySubjectGradeProfileID", MyUtils.ParseToInt(this.gvSubject["gradeprofileId", currRow].Value.ToString(), 0));
                    if (result.DataSet.Tables.Count > 0 && result.DataSet.Tables[0].Rows.Count > 0)
                    {
                        DataTable dt = result.DataSet.Tables[0];
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            CurriCulumnEntity entiy = new CurriCulumnEntity
                            {
                                Index = i + 1,
                                CurriCulumnDate = DateTime.Parse(dt.Rows[i]["dtplan"].ToString()).ToString("yyyy/MM/dd"),
                                CurriCulumnType = dt.Rows[i]["iClassTypePlanId"].ToString() == "1" ? "講義" : dt.Rows[i]["iClassTypePlanId"].ToString() == "2" ? "実習" : "",
                                CurriCulumnHours = MyUtils.ParseToInt(dt.Rows[i]["fUnitTimePlan"].ToString(), 0),
                                Content = dt.Rows[i]["vClassContents"].ToString(),
                                StartTime = dt.Rows[i]["dtStartTimePlan"].ToString(),
                                EndTime = dt.Rows[i]["dtEndTimePlan"].ToString(),
                                //RecorderPeople = "",
                                //Status = CommonFunction.GetStatusText(MyUtils.ParseToInt(dt.Rows[i]["istatusID"].ToString(), 0)),
                                ClassID = int.Parse(dt.Rows[i]["iClassId"].ToString()),
                                // SubjectGradeProfileID = int.Parse(dt.Rows[i]["iSubjectGradeProfileID"].ToString())

                            };
                            listCulumn.Add(entiy);
                        }
                    }
                }
                this.gvCurriCulumn.DataSource = listCulumn;
            }
            catch (Exception ex)
            {
                CommonFunction.ShowError("frmCurriCulumnList.ShowData", ex.Message);
            }
        }
        private void frmCurriCulumnList_Load(object sender, EventArgs e)
        {
            try
            {
                CommonFunction.SetDefaultYear(this.txtYear);
                CommonFunction.BindStatusCombox(this.cmbSelectStatus);
                CommonFunction.BindStatusCombox(this.cmbStatus2);
                CommonFunction.BindSelectedFlagCombox(this.cmbSubjectSelected);
                this.btnUpdate.Enabled = true;
                this.btnSentMail.Enabled = true;
            }
            catch (Exception ex)
            {
                CommonFunction.ShowError("frmCurriCulumnList.frmCurriCulumnList_Load", ex.Message);
            }
        }

        private void txtYear_TextChanged(object sender, EventArgs e)
        {
            //if (!string.IsNullOrEmpty(txtYear.Text.Trim()))
            //{
            //    int intYear = MyUtils.ParseToInt(txtYear.Text.Trim(), 1);
            //    if (intYear > 2000 && intYear < 2050)
            //    {
            //        this.cmbGrade.SelectedIndex = 0;
            //    }
            //}
        }

        private void btnSentMail_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.gvSubject.SelectedRows.Count <= 0)
                {
                    CommonFunction.ShowMsg("レコードを選択してください。");
                    return;
                }
                //if (this.gvCurriCulumn.SelectedRows.Count < 1) return;
                frmMail frmMail = new frmMail();
                string strSubjectGradeId = gvSubject["gradeprofileId", gvSubject.CurrentCell.RowIndex].Value.ToString();
                frmMail.setMondaiInfo(int.Parse(this.txtYear.Text), int.Parse(this.cmbGrade.Text), strSubjectGradeId, this.gvSubject["subjectName", this.gvSubject.CurrentCell.RowIndex].Value.ToString(), "", "");
                frmMail.ShowDialog();
            }
            catch (Exception ex)
            {
                CommonFunction.ShowError("frmCurriCulumnList.btnSentMail_Click", ex.Message);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.gvSubject.SelectedRows.Count <= 0)
                {
                    CommonFunction.ShowMsg("レコードを選択してください。");
                    return;
                }
                using (DBContext db = new DBContext())
                {
                    object[] paras = new object[4];
                    if (currCurriCulumnStatusId != 0) paras[0] = currCurriCulumnStatusId;
                    //update,xzg,2012/02/13,S
                    //if (currSubjectProfileId != 0) paras[1] = currSubjectProfileId;
                    //if (currSubjectGradeProfileId != 0) paras[2] = currSubjectGradeProfileId;

                    if (currSubjectGradeProfileId != 0) paras[1] = currSubjectGradeProfileId;
                    if (currSubjectProfileId != 0) paras[2] = currSubjectProfileId;
                    //update,xzg,2012/02/13,E
                    if (this.cmbStatus2.SelectedIndex != 0) paras[3] = int.Parse(this.cmbStatus2.SelectedValue.ToString());

                    if (currCurriCulumnStatusId != 0 && this.cmbStatus2.SelectedIndex == 0)
                    {
                        if (DialogResult.OK == MessageBox.Show("該当レコードを削除します。よろしいでしょうか？", "", MessageBoxButtons.OKCancel))
                        {
                            db.CallProcedure("usp_tbSTRcurriCulumnStatus_delete", currCurriCulumnStatusId);
                            CommonFunction.ShowMsg("更新しました");
                            ShowSubjectData();
                        }
                    }
                    else if (currCurriCulumnStatusId == 0 && this.cmbStatus2.SelectedIndex == 0)
                    {
                        CommonFunction.ShowMsg("状況を選択してください。");
                        return;
                    }
                    else
                    {
                        if (DialogResult.OK == MessageBox.Show("レコードを更新します。よろしいでしょうか？", "", MessageBoxButtons.OKCancel))
                        {
                            db.CallProcedure("usp_tbstrCurriCulumnStatus_add_edit", paras);
                            CommonFunction.ShowMsg("更新しました");
                            ShowSubjectData();
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                CommonFunction.ShowError("frmCurriCulumnList.btnUpdate_Click", ex.Message);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ShowListTeacher()
        {
            this.listTeacher.DataSource = null;
            if (gvCurriCulumn.RowCount > 0 && gvCurriCulumn.SelectedRows.Count > 0)
            {
                using (DBContext db = new DBContext())
                {
                    SqlResult result = db.CallProcedure("usp_Teacher_getByClass", MyUtils.ParseToInt(gvCurriCulumn["classId", gvCurriCulumn.CurrentCell.RowIndex].Value.ToString(), 0));
                    //SqlResult result = db.CallProcedure("usp_Teacher_getByClass", 1);
                    if (result.DataSet.Tables.Count > 0 && result.DataSet.Tables[0].Rows.Count > 0)
                    {
                        DataTable dt = result.DataSet.Tables[0];
                        this.listTeacher.DisplayMember = "vTeacherName";
                        this.listTeacher.DataSource = dt;
                    }
                }
            }
        }


        private void ClearGridData()
        {
            this.gvCurriCulumn.DataSource = null;
            this.gvSubject.DataSource = null;
            //List<CurriCulumnEntity> tempSource = new List<CurriCulumnEntity>();
            //this.gvCurriCulumn.DataSource = tempSource;
            //DataTable tempDt = this.gvSubject.DataSource as DataTable;
            //if (tempDt != null)
            //{
            //    tempDt.Rows.Clear();
            //    this.gvSubject.DataSource = tempDt;
            //}
            //this.gvList.Columns.Clear();
            //this.gvTeacher.Columns.Clear();

            this.btnSentMail.Enabled = false;
            this.btnUpdate.Enabled = false;
        }
        private void AddCulumnHader()
        {
            if (this.gvCurriCulumn.Columns.Count == 0)
            {
                this.gvCurriCulumn.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.No,
            this.date,
            this.type,
            this.hours,
            this.startTime,
            this.endTime,
            this.content,
            this.classId});
            }
            if (this.gvSubject.Columns.Count == 0)
            {
                this.gvSubject.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.Index,
            this.subjectName,
            this.status1,
            this.recordPepore,
            this.gradeprofileId,
            this.CurriCulumnStatusID});
            }
        }

        private void gvSubject_Click(object sender, EventArgs e)
        {
            try
            {
                if (gvSubject.SelectedRows.Count > 0)
                {
                    ShowCurriCulumnData();
                    ShowListTeacher();
                    this.cmbStatus2.Text = gvSubject["status1", gvSubject.CurrentCell.RowIndex].Value.ToString();
                    this.currCurriCulumnStatusId = MyUtils.ParseToInt(gvSubject["CurriCulumnStatusID", gvSubject.CurrentCell.RowIndex].Value.ToString(), 0);
                    this.currSubjectProfileId = MyUtils.ParseToInt(gvSubject["iSubjectProfileID", gvSubject.CurrentCell.RowIndex].Value.ToString(), 0);
                    this.currSubjectGradeProfileId = MyUtils.ParseToInt(gvSubject["gradeprofileId", gvSubject.CurrentCell.RowIndex].Value.ToString(), 0);
                    this.btnSentMail.Enabled = true;
                    this.btnUpdate.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                CommonFunction.ShowError("frmCurriCulumnList.gvSubject_Click", ex.Message);
            }
        }

        private void gvCurriCulumn_Click(object sender, EventArgs e)
        {
            ShowListTeacher();
        }

        private void gvSubject_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (gvSubject.SelectedRows.Count > 0)
                {
                    string fileName = gvSubject["SBOfile", gvSubject.CurrentCell.RowIndex].Value.ToString();
                    if (File.Exists(fileName))
                    {
                        System.Diagnostics.Process.Start(fileName);
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
    }
}
